import { getDictList } from '@/services/ant-design-pro/api';
import { useState, useCallback } from 'react';

type DictSubKeyItem = {
	value: string,
	text: string,
}

type DictKeyItem = {
	key: string,
	comment: string,
	list: DictSubKeyItem[],
}

export default () => {
	const dict:Array<DictKeyItem> = [];

	const store = {
		dict, // 数据字典
		async getAllDict(){
			store.dict = await getDictList();
		},
		getDictItem(key:string): DictKeyItem|null {
			const matchItem = store.dict.find(el=>el.key==key);
			if(matchItem){
				return matchItem;
			}

			console.warn('未找到匹配的数据字典项： key=' + key);
			return null;
		},
		getSubDictText(key:string, subkey:string): string|null{
			const matchItem = store.getDictItem(key);
			if(matchItem){
				const matchSubItem = matchItem.list.find(el=>el.value==subkey);
				if(matchSubItem){
					return matchSubItem.text;
				}
				console.warn('未找到匹配的数据字典子项： subkey=' + subkey);
			}
			return null;
		},
	};

	return store;
};
